<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Traits;

use App\Constants\SuccessCode;
use App\Model\ElectricOrder;
use App\Model\FuelOrder;
use App\Model\TelephoneOrder;
use App\Model\TelephoneOrderGroup;
use App\Model\TopExtractOrder;
use App\Model\Transact;
use App\Model\TransactOrder;
use App\Resource\BaseResource;
use Vtiful\Kernel\Excel;

trait ExcelTool
{

    public function exportOrder($where, $type)
    {
        $saveDir = BASE_PATH . '/public/excel/';
        if (! is_dir($saveDir)) {
            mkdir($saveDir);
        }
        $config = [
            'path' => $saveDir,
        ];
        $fileName = sprintf('订单-%s.xlsx', time());
        $excel = new Excel($config);

        $excel = $excel->fileName($fileName, '订单');

        $data = [];
        $titles = [];
        if ($type == 1) {

            $list = TelephoneOrder::where($where)->get();

            $data = $list->map(function ($order) {

                $rest_balance = '查询失败';
                if (is_array($order->phone_balance) && count($order->phone_balance) > 0) {
                    $rest_balance = $order->phone_balance[0]['rest_balance'];
                }
                return [
                    $order->letter . ($order->id % 1000),
                    $order->brand_name,
                    $order->real_mobile,
                    $order->price,
                    $rest_balance
                ];
            });

            $titles = ['序号', '运营商', '充值号码', '充值金额', '当前余额'];
        }elseif ($type == 2) {

            $list = FuelOrder::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '油卡充值',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    '套餐:' . $order->price . '  折扣:' . $order->discount,
                    $order->price * (100 - $order->discount) * 0.01,
                    $order->rate,
                    $order->real_amount,
                    $order->state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户','用户充值信息', '优惠信息', '汇率', '支付金额', '状态'];
        }elseif ($type == 3) {

            $list = ElectricOrder::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '电费充值',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    '套餐:' . $order->price . '  折扣:' . $order->discount,
                    $order->price * (100 - $order->discount) * 0.01,
                    $order->rate,
                    $order->real_amount,
                    $order->state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户', '用户充值信息', '优惠信息', '汇率', '支付金额', '状态'];
        }elseif ($type == 4) {

            $list = TelephoneOrderGroup::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '话费批量充值',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    '套餐:' . $order->price . '  折扣:' . $order->discount,
                    $order->price * (100 - $order->discount) * 0.01,
                    $order->rate,
                    $order->real_amount,
                    $order->state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户', '用户充值信息', '优惠信息', '汇率', '支付金额', '状态'];
        }elseif ($type == 5) {

            $list = TopExtractOrder::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '充值订单',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    $order->real_rate,
                    $order->real_amount,
                    $order->top_state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户', '汇率', '金额', '状态'];
        }elseif ($type == 6) {

            $list = TopExtractOrder::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '提现订单',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    $order->real_rate,
                    $order->real_amount,
                    $order->extract_state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户', '汇率', '金额', '状态'];
        }elseif ($type == 7) {

            $list = Transact::with('user')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '交易总单',
                    $order->user->nick_name . ' ' . $order->user->mobile,
                    $order->sell_account,
                    $order->sell_residue_account,
                    $order->unit_price,
                    $order->real_name,
                    $order->real_mobile,
                    $order->bank_card,
                    $order->bank_name,
                    $order->extract_state_format,
                ];
            });
            $titles = ['订单号', '类型', '用户', '出售金额', '出售剩余', '单价', '姓名', '手机号', '卡号', '开户行', '状态'];
        }elseif ($type == 8) {

            $list = TransactOrder::with('sellUser')->with('payUser')->where($where)->get();

            $data = $list->map(function ($order) {
                return [
                    $order->order_no,
                    '交易订单',
                    $order->payUser->nick_name . ' ' . $order->payUser->mobile,
                    $order->sellUser->nick_name . ' ' . $order->sellUser->mobile,
                    $order->unit_price,
                    $order->receipt_amount,
                    $order->usdt_amount,
                    $order->state_format,
                ];
            });
            $titles = ['订单号', '类型', '购买用户', '售出用户', '单价', '收款金额', '卖家扣款', '状态'];
        }

        $excel->header($titles)->data($data->toArray())->output();

        return new BaseResource(SuccessCode::SUCCESS, [
            'path' => env('HOST_NAME') . '/excel/' . $fileName,
        ]);
    }
}
